//@version=5
indicator("MA Matrix V3", shorttitle="MA Matrix V3", overlay=true)

// ==========================================
// 1. SETTINGS & GROUPS
// ==========================================
grp_gen   = "General Settings"
grp_disp  = "Table Row Display"
grp_short = "Short SMA (5, 8, 13)"
grp_mid   = "Mid EMA (21, 34)"
grp_long  = "Long EMA (55, 89, 100, 144)"
grp_debug = "Debug Settings"
grp_tol   = "Tolerance Settings"

lang         = input.string("Türkçe", "Language", options=["English", "Türkçe"], group=grp_gen, display = display.none)
table_pos    = input.string("Top Right", "Table Position", options=["Top Right", "Bottom Right", "Top Left", "Bottom Left", "Middle Right"], group=grp_gen, display = display.none)
table_transp = input.int(20, "Table Transparency (0-100)", minval=0, maxval=100, group=grp_gen, display = display.none)
txt_color    = input.color(color.white, "Text Color", group=grp_gen, display = display.none)

// Row Toggles
show_sig_row  = input.bool(true, "Show Signal Row", group=grp_disp)
show_rate_row = input.bool(true, "Show Star Rating Row", group=grp_disp)
show_prc_row  = input.bool(true, "Show Price Position Row", group=grp_disp)
show_mac_row  = input.bool(true, "Show Macro Row", group=grp_disp)
show_ema_row  = input.bool(true, "Show 200 EMA Row", group=grp_disp)
show_pls_row  = input.bool(true, "Show Pulse Rows", group=grp_disp)
show_liv_row  = input.bool(true, "Show Live Price Row", group=grp_disp)
show_tol_row  = input.bool(true, "Show Tolerance Row", group=grp_disp)
show_debug    = input.bool(true, "Show Debug Info", group=grp_debug, display = display.none)

c_green     = #089981
c_red       = #f23645
DARK_YELLOW = color.rgb(184, 134, 11)

is_tr() => lang == "Türkçe"

tol_mode  = input.string("Fixed", "Tolerance Mode", options=["Fixed", "ATR"], group=grp_tol, display = display.none)
tol_fixed = input.float(0.001, "Fixed Tolerance Ratio", minval=0.0001, maxval=0.05, step=0.0001, group=grp_tol, display = display.none)
atr_len   = input.int(14, "ATR Period", minval=1, group=grp_tol, display = display.none)
atr_mult  = input.float(0.1, "ATR Multiplier", minval=0.01, maxval=2.0, step=0.01, group=grp_tol, display = display.none)

show_m5   = input.bool(true, "Show SMA 5",     group=grp_short, display = display.none)
col_m5    = input.color(#2962FF, "SMA 5 Color",   group=grp_short, display = display.none)
show_m8   = input.bool(true, "Show SMA 8",     group=grp_short, display = display.none)
col_m8    = input.color(#00E5FF, "SMA 8 Color",   group=grp_short, display = display.none)
show_m13  = input.bool(true, "Show SMA 13",    group=grp_short, display = display.none)
col_m13   = input.color(#00C853, "SMA 13 Color", group=grp_short, display = display.none)

show_m21  = input.bool(true, "Show EMA 21",    group=grp_mid, display = display.none)
col_m21   = input.color(#FFAB00, "EMA 21 Color", group=grp_mid, display = display.none)
show_m34  = input.bool(true, "Show EMA 34",    group=grp_mid, display = display.none)
col_m34   = input.color(#D500F9, "EMA 34 Color", group=grp_mid, display = display.none)

show_m55  = input.bool(false, "Show EMA 55",    group=grp_long, display = display.none)
col_m55   = input.color(#F06292, "EMA 55 Color", group=grp_long, display = display.none)
show_m89  = input.bool(false, "Show EMA 89",    group=grp_long, display = display.none)
col_m89   = input.color(#FFEB3B, "EMA 89 Color", group=grp_long, display = display.none)
show_m100 = input.bool(false, "Show EMA 100",   group=grp_long, display = display.none)
col_m100  = input.color(#0091EA, "EMA 100 Color", group=grp_long, display = display.none)
show_m144 = input.bool(false, "Show EMA 144",   group=grp_long, display = display.none)
col_m144  = input.color(#FF6D00, "EMA 144 Color", group=grp_long, display = display.none)

show_m200 = input.bool(true, "Show EMA 200",   group="Macro", display = display.none)
col_m200  = input.color(#F23645, "EMA 200 Color", group="Macro", display = display.none)

// ==========================================
// 2. TIMEFRAME LOGIC
// ==========================================
get_htf(tf) =>
    switch tf
        "1"   => "5"
        "3"   => "15"
        "5"   => "20"
        "15"  => "60"
        "20"  => "60"
        "30"  => "60"
        "45"  => "120"
        "60"  => "240"
        "120" => "240"
        "240" => "D"
        "D"   => "W"
        "W"   => "M"
        => "M"

get_sub1_tf(tf) =>
    switch tf
        "5"   => "1"
        "15"  => "5"
        "20"  => "5"
        "60"  => "20"
        "120" => "60"
        "240" => "60"
        "D"   => "240"
        "W"   => "D"
        "M"   => "W"
        => tf

get_sub2_tf(tf) =>
    switch tf
        "15"  => "1"
        "20"  => "1"
        "60"  => "5"
        "120" => "20"
        "240" => "20"
        "D"   => "60"
        "W"   => "240"
        "M"   => "D"
        => tf

tf_disp(tf) =>
    switch tf
        "1"   => "1"
        "3"   => "3"
        "5"   => "5"
        "15"  => "15"
        "20"  => "20"
        "30"  => "30"
        "45"  => "45"
        "60"  => "60"
        "120" => "120"
        "240" => "240"
        "D"   => "D"
        "W"   => "W"
        "M"   => "M"
        => tf

get_sub1_disp(tf) =>
    switch tf
        "5"   => "1"
        "15"  => "5"
        "20"  => "5"
        "60"  => "20"
        "120" => "60"
        "240" => "60"
        "D"   => "240"
        "W"   => "D"
        "M"   => "W"
        => ""

get_sub2_disp(tf) =>
    switch tf
        "15"  => "1"
        "20"  => "1"
        "60"  => "5"
        "120" => "20"
        "240" => "20"
        "D"   => "60"
        "W"   => "240"
        "M"   => "D"
        => ""

// TF Definitions
htf = get_htf(timeframe.period)

// Current TF pulse requirement
c_sub1_req = get_sub1_tf(timeframe.period)
c_sub2_req = get_sub2_tf(timeframe.period)
c_sub1_dsp = get_sub1_disp(timeframe.period)
c_sub2_dsp = get_sub2_disp(timeframe.period)

// HTF pulse requirement
h_sub1_req = get_htf(htf)
h_sub2_req = get_htf(get_htf(htf))
h_sub1_dsp = tf_disp(get_htf(htf))
h_sub2_dsp = tf_disp(get_htf(get_htf(htf)))

// ==========================================
// 3. DECISION ENGINE
// ==========================================
get_status(m5, m8, m13, m21, m34, price, atr) =>
    tol    = tol_mode == "ATR" ? atr * atr_mult : price * tol_fixed
    k_up   = m5 > m8 and m8 > m13
    k_dn   = m5 < m8 and m8 < m13
    k_mix  = not k_up and not k_dn
    o_up   = m21 > (m34 + tol)
    o_dn   = m21 < (m34 - tol)
    o_flat = math.abs(m21 - m34) <= tol

    k_str = k_up ? (is_tr() ? "Kısalar↑" : "Shorts↑") : k_dn ? (is_tr() ? "Kısalar↓" : "Shorts↓") : (is_tr() ? "Kısalar?(Mix)" : "Shorts?(Mix)")
    o_str = o_up ? (is_tr() ? "Orta↑" : "Mid↑") : o_dn ? (is_tr() ? "Orta↓" : "Mid↓") : (is_tr() ? "Orta=(Yatay)" : "Mid=(Flat)")
    f_str = price > m5 ? "F > SMA5" : price > m13 ? "SMA5 >= F > SMA13" : price > m34 ? "SMA13 >= F > EMA34" : "F < EMA34"

    string d = is_tr() ? "İŞLEM YOK" : "NO TRADE"
    color  c = color.gray
    string r = k_str + "\n" + o_str + "\n" + f_str + "\n→ "

    if k_up and o_up
        if price > m5
            d := is_tr() ? "AL"      : "BUY",    c := c_green
        else if price <= m5 and price > m13
            d := is_tr() ? "TUT"     : "HOLD",   c := color.new(c_green, 40)
        else
            d := is_tr() ? "BEKLE ↑" : "WAIT ↑", c := DARK_YELLOW
    else if k_dn and o_dn
        if price < m5
            d := is_tr() ? "SAT"     : "SELL",    c := c_red
        else if price >= m5 and price < m13
            d := is_tr() ? "TUT ↓"   : "HOLD ↓",  c := color.new(c_red, 40)
        else
            d := is_tr() ? "BEKLE ↓" : "WAIT ↓",  c := DARK_YELLOW
    else if k_up and o_dn
        if price > m5
            d := is_tr() ? "BEKLE ↑"   : "WAIT ↑",   c := DARK_YELLOW
        else
            d := is_tr() ? "İŞLEM YOK" : "NO TRADE", c := color.gray
    else if k_dn and o_up
        if price < m5
            d := is_tr() ? "BEKLE ↓"   : "WAIT ↓",   c := DARK_YELLOW
        else
            d := is_tr() ? "İŞLEM YOK" : "NO TRADE", c := color.gray
    else if o_flat
        if k_mix
            d := is_tr() ? "SIKIŞMA"  : "SQUEEZE", c := color.gray
        else if k_up
            d := is_tr() ? "BEKLE ↑"  : "WAIT ↑",  c := DARK_YELLOW
        else if k_dn
            d := is_tr() ? "BEKLE ↓"  : "WAIT ↓",  c := DARK_YELLOW
    else if k_mix and o_up and price < m34
        d := is_tr() ? "İŞLEM YOK" : "NO TRADE", c := color.gray
    else if k_mix and o_dn and price > m34
        d := is_tr() ? "İŞLEM YOK" : "NO TRADE", c := color.gray

    r += d
    [d, c, r]

f_pos(c, p5, p13, p34) =>
    c > p5 ? "> SMA 5" : c > p13 ? "SMA5-SMA13" : c > p34 ? "SMA13-EMA34" : "< EMA 34"

// ==========================================
// 4. DATA FETCHING
// ==========================================
m5   = ta.sma(close, 5)
m8   = ta.sma(close, 8)
m13  = ta.sma(close, 13)
m21  = ta.ema(close, 21)
m34  = ta.ema(close, 34)
m55  = ta.ema(close, 55)
m89  = ta.ema(close, 89)
m100 = ta.ema(close, 100)
m144 = ta.ema(close, 144)
m200 = ta.ema(close, 200)
atr_v = ta.atr(atr_len)

c_m5   = m5[1],   c_m8  = m8[1],  c_m13  = m13[1]
c_m21  = m21[1],  c_m34 = m34[1]
c_m55  = m55[1],  c_m89 = m89[1], c_m100 = m100[1], c_m144 = m144[1], c_m200 = m200[1]
c_prc  = close[1]
c_atr  = atr_v[1]

[h5, h8, h13, h21, h34, h55, h89, h100, h144, h200, hc, h_atr] = request.security(
     syminfo.tickerid, htf,
     [ta.sma(close,5)[1], ta.sma(close,8)[1], ta.sma(close,13)[1],
      ta.ema(close,21)[1], ta.ema(close,34)[1], ta.ema(close,55)[1],
      ta.ema(close,89)[1], ta.ema(close,100)[1], ta.ema(close,144)[1],
      ta.ema(close,200)[1], close[1], ta.atr(atr_len)[1]],
     lookahead=barmerge.lookahead_on)

[live_hc, live_hm5, live_hm13, live_hm34] = request.security(
     syminfo.tickerid, htf,
     [close, ta.sma(close,5), ta.sma(close,13), ta.ema(close,34)],
     lookahead=barmerge.lookahead_off)

// Current TF Pulse 
[s1_5, s1_8, s1_13, s1_21, s1_34, s1_c, s1_atr] = request.security(
     syminfo.tickerid, c_sub1_req,
     [ta.sma(close,5)[1], ta.sma(close,8)[1], ta.sma(close,13)[1],
      ta.ema(close,21)[1], ta.ema(close,34)[1], close[1], ta.atr(atr_len)[1]],
     lookahead=barmerge.lookahead_on)

[s2_5, s2_8, s2_13, s2_21, s2_34, s2_c, s2_atr] = request.security(
     syminfo.tickerid, c_sub2_req,
     [ta.sma(close,5)[1], ta.sma(close,8)[1], ta.sma(close,13)[1],
      ta.ema(close,21)[1], ta.ema(close,34)[1], close[1], ta.atr(atr_len)[1]],
     lookahead=barmerge.lookahead_on)

// HTF Pulse
[hs1_5, hs1_8, hs1_13, hs1_21, hs1_34, hs1_c, hs1_atr] = request.security(
     syminfo.tickerid, h_sub1_req,
     [ta.sma(close,5)[1], ta.sma(close,8)[1], ta.sma(close,13)[1],
      ta.ema(close,21)[1], ta.ema(close,34)[1], close[1], ta.atr(atr_len)[1]],
     lookahead=barmerge.lookahead_on)

[hs2_5, hs2_8, hs2_13, hs2_21, hs2_34, hs2_c, hs2_atr] = request.security(
     syminfo.tickerid, h_sub2_req,
     [ta.sma(close,5)[1], ta.sma(close,8)[1], ta.sma(close,13)[1],
      ta.ema(close,21)[1], ta.ema(close,34)[1], close[1], ta.atr(atr_len)[1]],
     lookahead=barmerge.lookahead_on)

// ==========================================
// 5. STATUS CALCULATION & STAR RATING
// ==========================================
[c_sig, c_col, c_dbg] = get_status(c_m5, c_m8, c_m13, c_m21, c_m34, c_prc, c_atr)
[h_sig, h_col, h_dbg] = get_status(h5,   h8,   h13,   h21,   h34,   hc,   h_atr)

// Rating System Variables
var string last_c_sig = ""
var string prev_c_sig = ""
var int sig_duration = 0

var string last_h_sig = ""
var string prev_h_sig = ""
var int h_sig_duration = 0

if barstate.isconfirmed
    // Current TF Duration tracking
    if c_sig == last_c_sig
        sig_duration += 1
    else
        prev_c_sig := last_c_sig
        last_c_sig := c_sig
        sig_duration := 1
        
    // HTF Duration tracking
    if h_sig == last_h_sig
        h_sig_duration += 1
    else
        prev_h_sig := last_h_sig
        last_h_sig := h_sig
        h_sig_duration := 1

get_star_rating(sig, htf_sig, p, s_m5, s_m13, duration, prev_sig) =>
    float score = 0.0
    bool is_buy = sig == "AL" or sig == "BUY"
    bool is_sell = sig == "SAT" or sig == "SELL"
    
    if is_buy or is_sell
        // 1. HTF Match (+2)
        bool h_match = (is_buy and (htf_sig == "AL" or htf_sig == "BUY")) or (is_sell and (htf_sig == "SAT" or htf_sig == "SELL"))
        bool h_wait = str.contains(htf_sig, "BEKLE") or str.contains(htf_sig, "WAIT") or str.contains(htf_sig, "TUT") or str.contains(htf_sig, "HOLD")
        
        if h_match
            score += 2.0
        else if h_wait
            score += 1.0
            
        // 2. Duration (+1)
        if duration >= 5
            score += 1.0
        else if duration > 1
            score += 0.5
            
        // 3. Price Position (+1)
        if is_buy
            if p > s_m5
                score += 1.0
            else if p <= s_m5 and p > s_m13
                score += 0.5
        else if is_sell
            if p < s_m5
                score += 1.0
            else if p >= s_m5 and p < s_m13
                score += 0.5
                
        // 4. Previous Context (+1)
        bool from_wait = str.contains(prev_sig, "BEKLE") or str.contains(prev_sig, "WAIT")
        bool from_squeeze = str.contains(prev_sig, "SIKIŞMA") or str.contains(prev_sig, "SQUEEZE")
        bool from_opp = (is_buy and (prev_sig == "SAT" or prev_sig == "SELL")) or (is_sell and (prev_sig == "AL" or prev_sig == "BUY"))
        
        if from_squeeze
            score += 0.25
        else if from_wait
            score += 0.5
        else if from_opp
            score += 0.0
        else
            score += 1.0
            
    score

get_star_string(score) =>
    string stars = ""
    if score >= 5
        stars := "★★★★★"
    else if score >= 4.5
        stars := "★★★★½"
    else if score >= 4
        stars := "★★★★☆"
    else if score >= 3.5
        stars := "★★★½☆"
    else if score >= 3
        stars := "★★★☆☆"
    else if score >= 2.5
        stars := "★★½☆☆"
    else if score >= 2
        stars := "★★☆☆☆"
    else if score >= 1
        stars := "★☆☆☆☆"
    else
        stars := "☆☆☆☆☆"
    stars

// Pulse Calculations
[tc_p1_s, tc_p1_c, tc_p1_d] = get_status(s1_5,  s1_8,  s1_13,  s1_21,  s1_34,  s1_c,  s1_atr)
c_pulse1 = c_sub1_dsp == "" ? "-" : tc_p1_s
cp1_col  = c_sub1_dsp == "" ? color.gray : tc_p1_c

[tc_p2_s, tc_p2_c, tc_p2_d] = get_status(s2_5,  s2_8,  s2_13,  s2_21,  s2_34,  s2_c,  s2_atr)
c_pulse2 = c_sub2_dsp == "" ? "-" : tc_p2_s
cp2_col  = c_sub2_dsp == "" ? color.gray : tc_p2_c

[th_p1_s, th_p1_c, th_p1_d] = get_status(hs1_5, hs1_8, hs1_13, hs1_21, hs1_34, hs1_c, hs1_atr)
h_pulse1 = h_sub1_dsp == "" ? "-" : th_p1_s
hp1_col  = h_sub1_dsp == "" ? color.gray : th_p1_c

[th_p2_s, th_p2_c, th_p2_d] = get_status(hs2_5, hs2_8, hs2_13, hs2_21, hs2_34, hs2_c, hs2_atr)
h_pulse2 = h_sub2_dsp == "" ? "-" : th_p2_s
hp2_col  = h_sub2_dsp == "" ? color.gray : th_p2_c

// Calculate Scores
c_score = get_star_rating(c_sig, h_sig, c_prc, c_m5, c_m13, sig_duration, prev_c_sig)
h_score = get_star_rating(h_sig, th_p1_s, hc, h5, h13, h_sig_duration, prev_h_sig)

c_tol_val = tol_mode == "ATR" ? c_atr * atr_mult : c_prc * tol_fixed
h_tol_val = tol_mode == "ATR" ? h_atr * atr_mult : hc    * tol_fixed

// ==========================================
// 6. TABLE DESIGN
// ==========================================
pos_val = switch table_pos
    "Top Right"    => position.top_right
    "Bottom Right" => position.bottom_right
    "Top Left"     => position.top_left
    "Bottom Left"  => position.bottom_left
    => position.middle_right

tf_is_intraday  = timeframe.in_seconds() < 86400
htf_is_intraday = not (htf == "D" or htf == "W" or htf == "M")
cur_tf_lbl = tf_is_intraday  ? timeframe.period + "m" : timeframe.period
htf_tf_lbl = htf_is_intraday ? htf + "m"              : htf

var table panel = table.new(pos_val, 3, 30, border_width=1, border_color=color.new(color.gray, 70))

// Signal alignment indicator - defined outside barstate.islast to preserve color rendering
c_is_bull  = c_sig == (is_tr() ? "AL" : "BUY")  or c_sig == (is_tr() ? "TUT"   : "HOLD")
h_is_bull  = h_sig == (is_tr() ? "AL" : "BUY")  or h_sig == (is_tr() ? "TUT"   : "HOLD")
c_is_bear  = c_sig == (is_tr() ? "SAT" : "SELL") or c_sig == (is_tr() ? "TUT ↓" : "HOLD ↓")
h_is_bear  = h_sig == (is_tr() ? "SAT" : "SELL") or h_sig == (is_tr() ? "TUT ↓" : "HOLD ↓")

// ✓ aligned  |  ! conflict  |  ? neutral
c_same_dir  = (c_is_bull and h_is_bull) or (c_is_bear and h_is_bear)
c_conflict  = (c_is_bull and h_is_bear) or (c_is_bear and h_is_bull)
align_sym   = c_same_dir ? " ✓" : c_conflict ? " !" : " ?"
align_col   = c_same_dir ? color.rgb(0,230,118) : c_conflict ? color.rgb(255,152,0) : color.rgb(189,189,189)

if barstate.islast
    lbl_metric  = is_tr() ? "METRİK"    : "METRIC"
    lbl_current = is_tr() ? " (Mevcut)" : " (Current)"
    lbl_htf_s   = is_tr() ? " (Üst)"    : " (HTF)"
    lbl_signal  = (is_tr() ? "Sinyal" : "Signal") + align_sym
    lbl_rating  = is_tr() ? "Puan"      : "Rating"
    lbl_price   = is_tr() ? "Fiyat "    : "Price"
    lbl_macro   = is_tr() ? "Makro "    : "Macro "
    lbl_pulse   = is_tr() ? "Nabız"     : "Pulse"
    lbl_lprice  = is_tr() ? " Live"     : "Live"
    lbl_above   = is_tr() ? "ÜSTÜNDE"   : "ABOVE"
    lbl_below   = is_tr() ? "ALTINDA"   : "BELOW"
    lbl_tol     = "Tol. ["

    bg_c = color.new(color.black, table_transp)
    int r_idx = 0

    // Header Row
    table.cell(panel, 0, r_idx, lbl_metric,                bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
    table.cell(panel, 1, r_idx, cur_tf_lbl + lbl_current,  bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
    table.cell(panel, 2, r_idx, htf_tf_lbl + lbl_htf_s,    bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
    r_idx += 1

    // Signal Row
    if show_sig_row
        table.cell(panel, 0, r_idx, lbl_signal, bgcolor=bg_c, text_color=align_col, text_size=size.normal)
        table.cell(panel, 1, r_idx, c_sig + " (" + str.tostring(c_prc, format.mintick) + ")", bgcolor=c_col, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 2, r_idx, h_sig + " (" + str.tostring(hc,    format.mintick) + ")", bgcolor=h_col, text_color=txt_color, text_size=size.normal)
        r_idx += 1

    // Star Rating Row
    if show_rate_row
        string c_stars_str = (c_sig == "AL" or c_sig == "BUY" or c_sig == "SAT" or c_sig == "SELL") ? get_star_string(c_score) : "-"
        string h_stars_str = (h_sig == "AL" or h_sig == "BUY" or h_sig == "SAT" or h_sig == "SELL") ? get_star_string(h_score) : "-"
        table.cell(panel, 0, r_idx, lbl_rating, bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 1, r_idx, c_stars_str, bgcolor=bg_c, text_color=color.yellow, text_size=size.normal)
        table.cell(panel, 2, r_idx, h_stars_str, bgcolor=bg_c, text_color=color.yellow, text_size=size.normal)
        r_idx += 1

    // Price Position Row
    if show_prc_row
        table.cell(panel, 0, r_idx, lbl_price, bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 1, r_idx, f_pos(c_prc, c_m5, c_m13, c_m34), bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 2, r_idx, f_pos(hc, h5, h13, h34), bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        r_idx += 1

    // Macro Row
    if show_mac_row
        table.cell(panel, 0, r_idx, lbl_macro, bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 1, r_idx, c_m5 > c_m34 ? (is_tr() ? "BOĞA" : "BULL") : (is_tr() ? "AYI" : "BEAR"), bgcolor=bg_c, text_color=c_m5>c_m34 ? c_green : c_red, text_size=size.normal)
        table.cell(panel, 2, r_idx, h5   > h34   ? (is_tr() ? "BOĞA" : "BULL") : (is_tr() ? "AYI" : "BEAR"), bgcolor=bg_c, text_color=h5  >h34   ? c_green : c_red, text_size=size.normal)
        r_idx += 1

    // 200 EMA Row
    if show_ema_row
        table.cell(panel, 0, r_idx, "200 EMA", bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 1, r_idx, c_prc > c_m200 ? lbl_above : lbl_below, bgcolor=bg_c, text_color=c_prc>c_m200 ? c_green : c_red, text_size=size.normal)
        table.cell(panel, 2, r_idx, hc    > h200   ? lbl_above : lbl_below, bgcolor=bg_c, text_color=hc  >h200    ? c_green : c_red, text_size=size.normal)
        r_idx += 1

    // Pulse Rows
    if show_pls_row
        if c_sub1_dsp != "" or h_sub1_dsp != ""
            p1_lbl = lbl_pulse + " (" + (c_sub1_dsp==""?"-":c_sub1_dsp) + " / " + (h_sub1_dsp==""?"-":h_sub1_dsp) + ")"
            table.cell(panel, 0, r_idx, p1_lbl,   bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
            table.cell(panel, 1, r_idx, c_pulse1, bgcolor=bg_c, text_color=cp1_col,   text_size=size.normal)
            table.cell(panel, 2, r_idx, h_pulse1, bgcolor=bg_c, text_color=hp1_col,   text_size=size.normal)
            r_idx += 1

        if c_sub2_dsp != "" or h_sub2_dsp != ""
            p2_lbl = lbl_pulse + " (" + (c_sub2_dsp==""?"-":c_sub2_dsp) + " / " + (h_sub2_dsp==""?"-":h_sub2_dsp) + ")"
            table.cell(panel, 0, r_idx, p2_lbl,   bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
            table.cell(panel, 1, r_idx, c_pulse2, bgcolor=bg_c, text_color=cp2_col,   text_size=size.normal)
            table.cell(panel, 2, r_idx, h_pulse2, bgcolor=bg_c, text_color=hp2_col,   text_size=size.normal)
            r_idx += 1

    // Live Price Row
    if show_liv_row
        table.cell(panel, 0, r_idx, lbl_lprice, bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 1, r_idx, f_pos(close,   m5,       m13,      m34),       bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        table.cell(panel, 2, r_idx, f_pos(live_hc, live_hm5, live_hm13, live_hm34),  bgcolor=bg_c, text_color=txt_color, text_size=size.normal)
        r_idx += 1

    // Tolerance Row
    if show_tol_row
        table.cell(panel, 0, r_idx, lbl_tol + tol_mode + "]", bgcolor=color.new(color.gray,80), text_color=color.gray, text_size=size.normal)
        table.cell(panel, 1, r_idx, str.tostring(c_tol_val, format.mintick), bgcolor=color.new(color.gray,80), text_color=color.gray, text_size=size.normal)
        table.cell(panel, 2, r_idx, str.tostring(h_tol_val, format.mintick), bgcolor=color.new(color.gray,80), text_color=color.gray, text_size=size.normal)
        r_idx += 1

    // Debug Rows
    if show_debug
        table.cell(panel, 0, r_idx, "Debug Rule", bgcolor=color.gray, text_color=color.white, text_size=size.tiny)
        table.cell(panel, 1, r_idx, c_dbg,         bgcolor=bg_c,       text_color=c_col,       text_size=size.small)
        table.cell(panel, 2, r_idx, h_dbg,         bgcolor=bg_c,       text_color=h_col,       text_size=size.small)
        r_idx += 1

        c_vals = "SMA5: "    + str.tostring(c_m5,   format.mintick) +
                 "\nSMA8: "   + str.tostring(c_m8,   format.mintick) +
                 "\nSMA13: "  + str.tostring(c_m13,  format.mintick) +
                 "\nEMA21: "  + str.tostring(c_m21,  format.mintick) +
                 "\nEMA34: "  + str.tostring(c_m34,  format.mintick) +
                 "\nEMA55: "  + str.tostring(c_m55,  format.mintick) +
                 "\nEMA89: "  + str.tostring(c_m89,  format.mintick) +
                 "\nEMA100: " + str.tostring(c_m100, format.mintick) +
                 "\nEMA144: " + str.tostring(c_m144, format.mintick) +
                 "\nEMA200: " + str.tostring(c_m200, format.mintick) +
                 "\nATR: "    + str.tostring(c_atr,  format.mintick)

        h_vals = "SMA5: "    + str.tostring(h5,     format.mintick) +
                 "\nSMA8: "   + str.tostring(h8,     format.mintick) +
                 "\nSMA13: "  + str.tostring(h13,    format.mintick) +
                 "\nEMA21: "  + str.tostring(h21,    format.mintick) +
                 "\nEMA34: "  + str.tostring(h34,    format.mintick) +
                 "\nEMA55: "  + str.tostring(h55,    format.mintick) +
                 "\nEMA89: "  + str.tostring(h89,    format.mintick) +
                 "\nEMA100: " + str.tostring(h100,   format.mintick) +
                 "\nEMA144: " + str.tostring(h144,   format.mintick) +
                 "\nEMA200: " + str.tostring(h200,   format.mintick) +
                 "\nATR: "    + str.tostring(h_atr,  format.mintick)

        table.cell(panel, 0, r_idx, "Debug Vals", bgcolor=bg_c, text_color=color.gray,  text_size=size.tiny)
        table.cell(panel, 1, r_idx, c_vals,        bgcolor=bg_c, text_color=color.white, text_size=size.tiny)
        table.cell(panel, 2, r_idx, h_vals,        bgcolor=bg_c, text_color=color.white, text_size=size.tiny)

// ==========================================
// 7. DRAWINGS
// ==========================================
plot(show_m5   ? m5   : na, color=col_m5,   title="SMA 5",   linewidth=1)
plot(show_m8   ? m8   : na, color=col_m8,   title="SMA 8",   linewidth=1)
plot(show_m13  ? m13  : na, color=col_m13,  title="SMA 13",  linewidth=1)
plot(show_m21  ? m21  : na, color=col_m21,  title="EMA 21",  linewidth=1)
plot(show_m34  ? m34  : na, color=col_m34,  title="EMA 34",  linewidth=2)
plot(show_m55  ? m55  : na, color=col_m55,  title="EMA 55",  linewidth=1)
plot(show_m89  ? m89  : na, color=col_m89,  title="EMA 89",  linewidth=1)
plot(show_m100 ? m100 : na, color=col_m100, title="EMA 100", linewidth=1)
plot(show_m144 ? m144 : na, color=col_m144, title="EMA 144", linewidth=1)
plot(show_m200 ? m200 : na, color=col_m200, title="EMA 200", linewidth=2)